﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using XsClient.Common;
using XsClient.Parsers;

namespace XsClient.GUI
{
    /// <summary>
    /// WinSearchBook.xaml 的交互逻辑
    /// </summary>
    public partial class WinSearchBook : Panuon.UI.Silver.WindowX
    {
        List<XsSource> _Sources = null;
        public WinSearchBook()
        {
            InitializeComponent();
            Init();
        }
        private void Init()
        {
            try
            {
                _Sources = XsConfig.Current.Sources;
                int selectId = 0;
                int i = 0;
                foreach (var item in _Sources)
                {
                    cmbSource.Items.Add(item.Name + "/" + item.BaseUrl);
                    if (item.ID == XsConfig.Current.DefaultID)
                    {
                        selectId = i;
                    }
                    i++;
                }
                cmbSource.SelectedIndex = selectId;
            }
            catch { }
        }
        private void btnSearch_Click(object sender, RoutedEventArgs e)
        {

            try
            {
                var source = _Sources[cmbSource.SelectedIndex];
                BaseResolver resolver = ResolverFactory.GetResolver(source.ID);
                string key = txtKey.Text.Trim();
                if (!string.IsNullOrEmpty(key))
                {
                    var books0 = resolver.Seach(key);
                    ShowBooks(books0);
                }
                else
                {
                    var books = resolver.FindBooks(resolver.Source.BaseUrl);

                    ShowBooks(books);
                }

            }
            catch (Exception ex)
            {
                NewLife.Log.XTrace.WriteException(ex);
            }
        }
        public void ShowBooks(List<BookInfo> books)
        {
            try
            {
                if (books != null)
                {
                    this.dgMain.ItemsSource = books;
                }
            }
            catch { }
        }

        private void dgBtnJoin_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                BookInfo info = this.dgMain.SelectedItem as BookInfo;
                if (info != null)
                {

                    if (AppHelper.Instance.BookList.Books.Count(p => p != null && p.Name == info.Name && p.BaseUrl == info.BaseUrl) <1)
                    {
                        info.Name = GetBookName(info.Name);
                        AppHelper.Instance.BookList.Books.Add(info);
                    }
                    AppHelper.Instance.BookList.Save();
                }
            }
            catch { }
        }
        private string GetBookName(string name)
        {
            var books = AppHelper.Instance.BookList.Books;
            int i = 0;
            string dstName = name;
            while (books.Count(p => p!=null&& p.Name == dstName) >0)
            {
                dstName = name + (i++).ToString();
            }
            return dstName;
        }
    }
}
